package com.koujun.meinv.server.caipiao;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;

public class Downloader {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws Exception {
		String httpURL =URLDecoder.decode("magnet:?xt=urn:btih:e46169bb957265e1a96a7abd5794e69ae6ea6bdf&dn=%5BMyFirstSexTeacher%5D+Holly+West+%2815975+%2F+04.08.2013%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80");
		System.out.println(httpURL);
		URL url = new URL(httpURL);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		getStreamContents(connection.getInputStream(), "UTF-8");
		connection.disconnect();
	}

	public static void getStreamContents(InputStream is, String encoding) throws IOException {
		final char[] buffer = new char[0x10000];
		StringBuffer out = new StringBuffer();
		Reader in = new InputStreamReader(is, encoding);
		FileWriter writer=new FileWriter(new File("a.torrent"));
		int read;
		do {
			read = in.read(buffer, 0, buffer.length);
			if (read > 0) {
				out.append(buffer, 0, read);
				writer.write(buffer, 0, read);
			}
		} while (read >= 0);
		in.close();
		writer.close();
	}
}
